package cn.honor.test;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 31464 on 2021/10/17.
 */
public class Example2 {

    private static final String orgData="I MusicA Pop\n" +
            "I MusicB Pop\n" +
            "I MusicC Blue\n" +
            "I MusicD Blue\n" +
            "I MusicE UnkownStyle\n" +
            "I MusicF Pop\n" +
            "P MusicA\n" +
            "P MusicB\n" +
            "B MusicC\n" +
            "P MusicF\n" +
            "B MusicD\n" +
            "B MusicE\n";


    public static void main(String[] args) {
        String[] split = orgData.split("\n");

        //原始数据集转为为LIST
        List<String> orgDataLst = Arrays.asList(split);

        //根据歌曲导入（I开头的）数据，将歌曲库进行整理
        List<SongEntity> songDBList = orgDataLst.stream().filter(x -> x != null && x.startsWith("I")).map(x -> {
            String[] beans = x.split(" ");
            SongEntity entity = new SongEntity();
            entity.songName = beans[1];
            entity.songType = beans[2];
            entity.score = 0;
            return entity;
        }).collect(Collectors.toList());

        //收集播放库以P,B开头

        List<String> playDBList = orgDataLst.stream().filter(x -> x != null && (x.startsWith("B")||x.startsWith("P"))).collect(Collectors.toList());

        //计算得分
        for(int i=0;i<playDBList.size();i++){
            String[] split1 = playDBList.get(i).split(" ");
            String playType = split1[0];
            String songName = split1[1];

            for(int j=0;j<songDBList.size();j++){
                SongEntity entity = songDBList.get(i);
                if(StringUtils.equalsIgnoreCase(songName,entity.songName)){
                    if(StringUtils.equalsIgnoreCase(playType,"P")){
                        calcScore(entity.songType,1,songDBList);
                    }else if(StringUtils.equalsIgnoreCase(playType,"B")){
                        calcScore(entity.songType,-1,songDBList);
                    }
                }
            }
        }

        Collections.sort(songDBList, new Comparator<SongEntity>() {
            @Override
            public int compare(SongEntity o1, SongEntity o2) {
                int o=o1.score- o2.score;
                if(o>0){
                    return -1;
                }else if(o<1){
                    return 1;
                }else{
                    return 0;
                }
            }
        });

        System.out.println(songDBList);

    }



    public static void calcScore(String type,int score,List<SongEntity> songEntities){
        for(int i=0;i<songEntities.size();i++){
            SongEntity entity = songEntities.get(i);
            if(StringUtils.equalsIgnoreCase(entity.songType,"UnkownStyle")){
                entity.score+=score;
                break;
            }else if(StringUtils.equalsIgnoreCase(entity.songType,type)){
                //如果不是UnkownStyle就对同类型的数据都加分或者减分
                entity.score+=score;
            }
        }
    }

    /**
     * 歌曲实体类
     */
    public static class SongEntity{
        public String songName;//歌曲名称
        public String songType;//歌曲类型
        public Integer score;//歌曲分数

        @Override
        public String toString() {
            return "songName:"+songName+" songType:"+songType+" score:"+score +"\n";
        }
    }

}



